package webshop;

import database.DBConnector;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Product {

    private PriceCalculator calc = new PriceCalculator();
    private DBConnector dbc = DBConnector.getDBC();
    private String id;
    private String name;
    private double purchasePrice;
    private int salesprice;
    private String[] departments;
    private String[] sections;
    private String[] brands;
    private String[] productTypes;
    private String[][] specifications;
    private Car[] carMatches;

    public Product(String id, String name, double purchasePrice, String[] departments,
            String[] sections, String[] brands, String[] productTypes, String[][] specifications) {
        this.id = id;
        this.name = name;
        this.purchasePrice = purchasePrice;
        this.departments = departments;
        this.sections = sections;
        this.brands = brands;
        this.productTypes = productTypes;
        this.specifications = specifications;

        purchasePrice = calc.calculatePrice(purchasePrice);
        salesprice = (int) Math.round(purchasePrice);
    }

    public void addProduct() {



        addMatches();
    }

    private void addMatches() {
        for (int i = 0; i < carMatches.length; i++) {
            try {
                String make = carMatches[i].getMake();
                String model = carMatches[i].getModel();
                Integer year = carMatches[i].getYear();
                String engine = carMatches[i].getEngine();
                String submodel = "null";
                boolean makeExists = false;
                boolean modelExists = false;
                boolean yearExists = false;
                boolean engineExists = false;
                ResultSet rsMake = dbc.doQuery("SELECT makeID FROM make WHERE makeID='" + make + "';");
                while (rsMake.next()) {
                    makeExists = true;
                }
                if (makeExists) {
                    dbc.doUpdate("INSERT INTO Make VALUES('" + make + "','" + make + "');");
                }
                ResultSet rsModel = dbc.doQuery("SELECT modelID FROM model WHERE modelID='" + model + "';");
                while (rsModel.next()) {
                    modelExists = true;
                }
                if (modelExists) {
                    dbc.doUpdate("INSERT INTO Model VALUES('" + model + "','" + model + "');");
                }
                ResultSet rsYear = dbc.doQuery("SELECT year FROM year WHERE year='" + year + "';");
                while (rsYear.next()) {
                    yearExists = true;
                }
                if (yearExists) {
                    dbc.doUpdate("INSERT INTO year VALUES('" + year + "');");
                }
                ResultSet rsEngine = dbc.doQuery("SELECT enginesize FROM enginesizze WHERE enginesize='" + engine + "';");
                while (rsEngine.next()) {
                    engineExists = true;
                }
                if (engineExists) {
                    dbc.doUpdate("INSERT INTO enginesize VALUES('" + engine + "');");
                }
                String carID = make + "-" + model + "-" + "-" + submodel + "-" + year + "-" + engine;
                dbc.doUpdate("INSERT INTO Cars VALUES('" + carID + "','" + make + "','" + model + "'," + submodel + ",'" + year + "','" + engine + "');");
                String matchID = id + "-" + carID;
                dbc.doUpdate("INSERT INTO matches VALUES('" + matchID + "','" + carID + "','" + id + "');");
            } catch (SQLException ex) {
                Logger.getLogger(Product.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
